<template>
  <div>
    <div class="pageTitleWhole">
      <a-breadcrumb>
        <a-breadcrumb-item>运营报表</a-breadcrumb-item>
      </a-breadcrumb>
    </div>
    <div id="takeoutMain">
      <a-form-model layout="inline" :model="form" style="margin-top:0px;">
        <a-form-model-item label="船名">
          <a-select v-model="form.shipId" style="width:200px;">
            <a-select-option value>全部</a-select-option>
            <a-select-option
              v-for="(item,index) in shipNameList"
              :key="index"
              :value="item.id"
            >{{item.name}}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="航次">
          <a-input v-model="form.voyageNumberLike" />
        </a-form-model-item>
        <a-form-model-item label="起航日期">
          <a-range-picker @change="onChange" :value="timeValue" style="width:250px">
            <a-icon slot="suffixIcon" type="calendar" />
          </a-range-picker>
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" @click="check">查询</a-button>
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" @click="excelExport">导出Excel</a-button>
        </a-form-model-item>
      </a-form-model>
      <a-card class="porerat_shujutz">
        <a-row :gutter="[16]">
          <a-col :span="3" class="assent">
            <div class="assent_mes">总航次</div>
            <div class="assent_shu">{{statistics.voyageNum}}</div>
          </a-col>
          <a-col :span="3" class="assent">
            <div class="assent_mes">总实装量TEU</div>
            <div class="assent_shu">{{statistics.actualLoadingCapacity}}</div>
          </a-col>
          <a-col :span="3" class="assent">
            <div class="assent_mes">仓位完成率</div>
            <div class="assent_shu">{{statistics.positionCompletionRate}}</div>
          </a-col>
          <a-col :span="3" class="assent">
            <div class="assent_mes">集装箱运营总利润（元）</div>
            <div class="assent_shu">{{statistics.containerProfit}}</div>
          </a-col>
          <a-col :span="4" class="assent">
            <div class="assent_mes">船舶运营总利润（元）</div>
            <div class="assent_shu">{{statistics.voyageProfit}}</div>
          </a-col>
          <a-col :span="4" class="assent">
            <div class="assent_mes">佳华船舶运营总补贴（元）</div>
            <div class="assent_shu">{{statistics.shipRentingAmount}}</div>
          </a-col>
          <a-col :span="4">
            <div class="assent_mes">总运营利润合计（元）</div>
            <div class="assent_shu">{{statistics.profit}}</div>
          </a-col>
        </a-row>
      </a-card>
      <a-table
        :columns="columns"
        :loading="loading"
        :data-source="data"
        style="margin-top:25px;"
        :scroll="{x:2800}"
        :pagination="pagination"
        :row-key="(record,index) => index"
      >
        <span
          slot="action"
          slot-scope="text, record"
          :style="'display: flex; align-items: center;'"
        >
          <a @click="detail(record)">详情</a>
        </span>
      </a-table>
    </div>
  </div>
</template>
<script>
import { commonExport } from '@/api/request'
import { queryShipNameList } from '@/api'
import moment from 'moment'
import {
  queryOperatingReportList,
  queryOperatingReportListStatistics
} from '@/api/dataBI'
const columns = [
  {
    title: '序号',
    width: 80,
    customRender: (text, record, index) => {
      return index + 1
    }
  },
  {
    title: '船名',
    dataIndex: 'shipName'
  },
  {
    title: '航次',
    dataIndex: 'voyageNumber'
  },
  {
    title: '起航日期',
    dataIndex: 'departureDate'
  },
  {
    title: '装载量（TEU）',
    dataIndex: 'loadingCapacity'
  },
  {
    title: '实装重箱（TEU）',
    dataIndex: 'heavyActualLoadingCapacity'
  },
  {
    title: '实装空箱（TEU）',
    dataIndex: 'emptyActualLoadingCapacity'
  },
  {
    title: '仓位完成率',
    dataIndex: 'positionCompletionRateStr'
  },
  {
    title: '集装箱运营利润（元）',
    dataIndex: 'containerProfit',
    customRender: (text, record, index) => {
      return record.containerProfit.toFixed(2)
    }
  },
  {
    title: '船舶运营利润（元）',
    dataIndex: 'voyageProfit',
    customRender: (text, record, index) => {
      return record.voyageProfit.toFixed(2)
    }
  },
  {
    title: '佳华船舶运营补贴（元）',
    dataIndex: 'shipRentingAmount',
    customRender: (text, record, index) => {
      return record.shipRentingAmount.toFixed(2)
    }
  },
  {
    title: '总运营利润（元）',
    dataIndex: 'profit',
    customRender: (text, record, index) => {
      return record.profit.toFixed(2)
    }
  },
  // {
  //   title: '集装箱应收金额（元）',
  //   dataIndex: 'containerReceivableAmount',
  //   customRender: (text, record, index) => {
  //     return record.containerReceivableAmount.toFixed(2)
  //   }
  // },
  // {
  //   title: '集装箱应付金额（元）',
  //   dataIndex: 'containerPayableAmount',
  //   customRender: (text, record, index) => {
  //     return record.containerPayableAmount.toFixed(2)
  //   }
  // },
  // {
  //   title: '集装箱毛利（元）',
  //   dataIndex: 'containerProfit',
  //   customRender: (text, record, index) => {
  //     return record.containerProfit.toFixed(2)
  //   }
  // },
  // {
  //   title: '航次应收金额（元）',
  //   dataIndex: 'voyageReceivableAmount',
  //   customRender: (text, record, index) => {
  //     return record.voyageReceivableAmount.toFixed(2)
  //   }
  // },
  // {
  //   title: '航次应付金额（元）',
  //   dataIndex: 'voyagePayableAmount',
  //   customRender: (text, record, index) => {
  //     return record.voyagePayableAmount.toFixed(2)
  //   }
  // },
  // {
  //   title: '航次毛利（元）',
  //   dataIndex: 'voyageProfit',
  //   customRender: (text, record, index) => {
  //     return record.voyageProfit.toFixed(2)
  //   }
  // },
  // {
  //   title: '佳华承租费（元）',
  //   dataIndex: 'shipRentingAmount',
  //   customRender: (text, record, index) => {
  //     return record.shipRentingAmount.toFixed(2)
  //   }
  // },
  // {
  //   title: '总毛利（元）',
  //   dataIndex: 'profit',
  //   customRender: (text, record, index) => {
  //     return record.profit.toFixed(2)
  //   }
  // },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action'
    }
  }
]
export default {
  data () {
    return {
      shipNameList: [],
      form: {
        voyageNumberLike: '',
        shipId: '',
        startDate: '',
        endDate: ''
      },
      pageNum: 1,
      pageSize: 10,
      loading: true,
      data: [],
      columns,
      pagination: {},
      statistics: {},
      timeValue: []
    }
  },
  methods: {
    shipNameListRequest (e) {
      queryShipNameList(e)
        .then(res => {
          this.shipNameList = res.data
        })
        .catch(rs => {})
    },
    shipReportListRequest (e) {
      this.loading = true
      queryOperatingReportList(e)
        .then(res => {
          this.data = res.data.list
          let currentPage = this.pageNum
          this.$route.meta.s = e
          this.pagination = {
            current: currentPage,
            pageSize: res.data.pageSize,
            total: res.data.total,
            showQuickJumper: false,
            showSizeChanger: true,
            pageSizeOptions: ['10', '30', '50', '100', '500'],
            showTotal: total =>
              `共${total}条记录 第${currentPage}/${res.data.pages}页`,
            onChange: pageNum => {
              this.pageNum = pageNum
              let paramData = {
                pageSize: this.pageSize,
                pageNum: this.pageNum,
                ...this.form
              }
              this.shipReportListRequest(paramData)
            },
            onShowSizeChange: (current, size) => {
              this.pageNum = current
              this.pageSize = size
              let paramData = {
                pageSize: this.pageSize,
                pageNum: this.pageNum,
                ...this.form
              }
              this.shipReportListRequest(paramData)
            }
          }
          this.loading = false
        })
        .catch(rs => {})
    },
    shipStatisticsRequest (e) {
      queryOperatingReportListStatistics(e)
        .then(res => {
          // // console.log(res.data)
          res.data.positionCompletionRate = `${(res.data.positionCompletionRate * 100).toFixed(2)}%`
          res.data.containerProfit = res.data.containerProfit.toFixed(2)
          res.data.voyageProfit = res.data.voyageProfit.toFixed(2)
          res.data.shipRentingAmount = res.data.shipRentingAmount.toFixed(2)
          res.data.profit = res.data.profit.toFixed(2)
          this.statistics = res.data
        })
        .catch(rs => {})
    },
    onChange (date, dateString) {
      this.timeValue = date
      this.form.startDate = dateString[0]
      this.form.endDate = dateString[1]
    },
    check () {
      this.pageNum = 1
      this.shipStatisticsRequest({
        ...this.form
      })
      this.shipReportListRequest({
        ...this.form,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    excelExport () {
      commonExport('/containerReport/exportOperatingReportList', {
        ...this.form,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        columns: this.columns
      })
    },
    detail (e) {
      this.$router.push(`operationReport/detail/${e.voyageId}`)
    }
  },
  mounted () {
    this.shipNameListRequest({})
    let originObj = this.$route.meta.s
    this.listAssign(this.form, originObj)
    if (originObj) {
      this.pageSize = originObj.pageSize
      this.pageNum = originObj.pageNum
      this.form.startDate = originObj.startDate
      this.form.endDate = originObj.endDate
      if (originObj.startDate) {
        this.timeValue = [moment(originObj.startDate), moment(originObj.endDate)]
      } else {
        this.timeValue = []
      }
    }
    this.shipStatisticsRequest({
      ...this.form
    })
    this.shipReportListRequest({
      ...this.form,
      pageNum: this.pageNum,
      pageSize: this.pageSize
    })
  },
  beforeRouteEnter (to, from, next) {
    if (from.name !== 'JHoperationReportDetail' || to.query.f !== '1') {
      to.meta.s = null
    }
    next()
  }
}
</script>

<style lang="less" scoped>
#takeoutMain {
  padding: 24px;
  margin: 24px;
  background: #fff;
  min-height: 700px;
}
</style>
<style>
.porerat_shujutz {
  text-align: center;
  margin-top: 24px;
  overflow-x: scroll;
}
.porerat_shujutz .ant-card-body {
  width: 1488px;
}
</style>
